from PySide6.QtWidgets import QPushButton, QWidget, QLabel, QHBoxLayout
from PySide6.QtCore import Qt, QPoint
from PySide6.QtGui import QIcon, QMouseEvent

from util import UiUtil


class TitleBar(QWidget):
    def __init__(self, parent: QWidget):
        super().__init__(parent)
        self.main_window = parent

        # 设定组件布局
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)

        # 加载ui
        ui = UiUtil.loadUiFile("ui/TitleBar.ui")
        layout.addWidget(ui)

        # 窗口拖动变量
        self.drag_position = QPoint()

        # 添加自定义按钮
        self.min_button: QPushButton = ui.min_btn
        self.min_button.setIcon(QIcon("icon/WindowMinimize.svg"))
        self.max_restore_button: QPushButton = ui.max_restore_btn
        self.max_restore_button.setIcon(QIcon("icon/WindowMaximize.svg"))
        self.close_button: QPushButton = ui.close_btn
        self.close_button.setIcon(QIcon("icon/WindowClose.svg"))
        self.help_button: QPushButton = ui.help_btn
        self.help_button.setIcon(QIcon("icon/Help.svg"))
        self.title_label: QLabel = ui.title_label
        self.label_icon: QLabel = ui.label_icon

        # 从QIcon获取QPixmap并设置给QLabel
        icon = QIcon("logo.png")
        pixmap = icon.pixmap(20, 20)
        self.label_icon.setPixmap(pixmap)

        # 设置按钮样式
        for [index, btn] in enumerate([self.min_button, self.max_restore_button,
                                       self.close_button, self.help_button]):
            btn.setStyleSheet(f"""
                QPushButton {{
                    background-color: transparent;
                    border: none;
                    color: white;
                    font-size: 16px;
                    border-radius: 0px;
                }}
                QPushButton:hover {{
                    background-color: {'red' if index==2 else '#4f5b62'};
                }}
            """)

        self.title_label.setStyleSheet("""
            font-size: 14px;
        """)

        self.setStyleSheet("""
            background-color: #3d444b;
        """)

        # 按钮点击事件
        self.close_button.clicked.connect(self.main_window.close)
        self.min_button.clicked.connect(self.main_window.showMinimized)
        self.max_restore_button.clicked.connect(self.toggle_maximize)
        self.help_button.clicked.connect(self.show_readme)

    def toggle_maximize(self):
        if self.main_window is None:
            return

        if self.main_window.isMaximized():
            self.main_window.showNormal()
            self.max_restore_button.setIcon(QIcon("icon/WindowMaximize.svg"))
        else:
            self.main_window.showMaximized()
            self.max_restore_button.setIcon(QIcon("icon/WindowRestore.svg"))

    def mouseDoubleClickEvent(self, event: QMouseEvent):
        if event.button() == Qt.MouseButton.LeftButton:
            self.toggle_maximize()
            event.accept()

    def mousePressEvent(self, event: QMouseEvent):
        if self.main_window is None:
            return

        if event.button() == Qt.MouseButton.LeftButton:
            self.drag_position = event.globalPosition().toPoint() - \
                self.main_window.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self, event: QMouseEvent):
        if self.main_window is None:
            return

        if event.buttons() & Qt.MouseButton.LeftButton:
            if self.main_window.isMaximized():
                # 获得原始比例
                tmpPos = event.globalPosition().toPoint() - \
                    self.main_window.frameGeometry().topLeft()
                scale = abs(tmpPos.x()) / \
                    self.main_window.frameGeometry().width()
                # 新的宽度
                new_width = self.main_window.normalGeometry().size().width()
                # 还原
                self.main_window.showNormal()
                self.max_restore_button.setIcon(
                    QIcon("icon/WindowMaximize.svg"))
                # 设定新的拖放位置
                tmpPos.setX(new_width * scale)
                self.drag_position = tmpPos

            # 移动窗口
            self.main_window.move(
                event.globalPosition().toPoint() - self.drag_position)
            event.accept()

    def set_title(self, title: str):
        """设置标题"""
        self.title_label.setText(title)

    def set_min_button_visible(self, show: bool):
        """设置按钮可见性"""
        self.min_button.setVisible(show)

    def set_max_button_visible(self, show: bool):
        """设置按钮可见性"""
        self.max_restore_button.setVisible(show)

    def set_close_button_visible(self, show: bool):
        """设置按钮可见性"""
        self.close_button.setVisible(show)

    def set_help_button_visible(self, show: bool):
        """设置按钮可见性"""
        self.help_button.setVisible(show)

    def show_readme(self):
        """显示帮助文件"""
        print("show help")
